package com.xianzw.mnfun.web.controller.front;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xianzw.mnfun.model.domain.Post;
import com.xianzw.mnfun.model.dto.HaloConst;
import com.xianzw.mnfun.model.enums.BlogProperties;
import com.xianzw.mnfun.service.PostService;
import com.xianzw.mnfun.web.controller.core.BaseController;

/**
 * 福利资源
 * @author xianzw
 *
 */
@Controller
@RequestMapping("/welfareRes")
public class FrontWelfareResController extends BaseController{

    @Autowired
    private PostService postService;
	
	/**
     * 福利资源
     *
     * @param model model
     * @return 模板路径
     */
    @GetMapping
    public String welfareRes(Model model) {
        return this.index(model, 1);
    }

    /**
     * 首页分页
     *
     * @param model model
     * @param page  当前页码
     * @param size  每页数量
     * @return 模板路径/themes/{theme}/index
     */
    @GetMapping(value = "welfareRes/{page}")
    public String index(Model model,
                        @PathVariable(value = "page") Integer page) {
        Sort sort = new Sort(Sort.Direction.DESC, "postDate");
        //默认显示10条
        Integer size = 10;
        //尝试加载设置选项，用于设置显示条数
        if (!StringUtils.isBlank(HaloConst.OPTIONS.get(BlogProperties.INDEX_POSTS.getProp()))) {
            size = Integer.parseInt(HaloConst.OPTIONS.get(BlogProperties.INDEX_POSTS.getProp()));
        }
        //所有文章数据，分页
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        Page<Post> posts = postService.findPostByStatus(0,"welfareRes",pageable);
        if (null == posts) {
            return this.renderNotFound();
        }
        model.addAttribute("posts", posts);
        return this.render("welfareRes/welfareRes");
    }
	
}
